import Toast from 'tdesign-miniprogram/toast/index';
import {fetchMatchList} from '../../../utils/request'

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    commodityName: '',
    tabIndex: 1,
    demandId: null
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 1,
  },

  handleSearchChange(e) {
    this.setData({
      commodityName: e.detail.value
    })
    this.loadGoodsList(true);
  },

  onShow() {
  },

  onLoad(query) {
    const demandId = query.demandId;
    this.setData({
      demandId: demandId
    })
    this.init();
  },
  onReady() {
    
  },

  onReachBottom() {
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  async loadHomePage() {
    this.setData({
      pageLoading: true,
    });
    const res = await fetchMatchList(this.data.demandId);
    const d = res.data ? res.data : [];
    const list = d.map(item => {
      return {
        spuId: item.id,
        thumb: item.imageUrlList[0],
        title: item.name,
        typeList: item.typeList
      }
    });
    this.setData({
      goodsList: list,
      pageLoading: false,
    });
  },

  goodListClickHandle(e) {
    const { index } = e.detail;
    const { spuId } = this.data.goodsList[index];
    wx.setStorageSync('demand-to-order', 1);
    wx.setStorageSync('demandId', this.data.demandId);
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`,
    });
  },

  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    wx.navigateTo({ url: '/pages/goods/search/index' });
  },

  navToActivityDetail({ detail }) {
    const { index: promotionID = 0 } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
});
